<template>
  <div>
    <!--    参与单位管理页面-->
    <div class="search">
      <el-form :inline="true" :model="form" ref="formRef">
        <el-form-item label="参与单位" style="margin-left: 10px">
          <el-input v-model="unitName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="单位类型" style="margin-left: 10px">
          <el-input v-model="unitCategory" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" plain style="margin-left: 10px" @click="load(1)">查询</el-button>
          <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--    新增，批量删除按钮-->
    <div class="operation">
      <el-button type="primary" plain @click="handleAdd">新增</el-button>
    </div>

    <div class="table">
      <el-table :data="tableData" stripe border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="unitId" label="序号" width="80" align="center" sortable></el-table-column>
        <el-table-column prop="unitName" label="单位名称" show-overflow-tooltip>
          <!--          设置单位名称那一列的数据点击跳转-->
          <!-- <template slot-scope="scope">
            <router-link to="/click">{{ scope.row.unitName }}</router-link>
          </template> -->
          <template slot-scope="scope">
            <el-link type="primary" plain @click="handleEdit(scope.row)">{{ scope.row.unitName }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="unitCategory" label="单位类别" show-overflow-tooltip></el-table-column>
        <el-table-column prop="creditCode" label="社会信用代码"></el-table-column>
        <el-table-column prop="contact" label="联系人"></el-table-column>
        <el-table-column prop="contactPhone" label="联系方式"></el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template v-slot="scope">
            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="del(scope.row.unitId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域-->
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
          :page-sizes="[1, 5, 10, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Notice",
  data() {
    return {
      tableData: [],  // 所有的数据
      //当前的页数
      pageNum: 1,
      //当前每页显示多少条数据
      pageSize: 20,
      total: 0,
      title: null,
      unitName: "",
      unitCategory: "",
      form: {},
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      // rules: {
      //   title: [
      //     {required: true, message: '请输入标题', trigger: 'blur'},
      //   ],
      // },
      ids: [],
      noticeData: [],
    }
  },
  created() {
    this.load(1)
    this.loadNotice()
  },
  methods: {
    handleAdd() {   // 新增数据
      this.form = {}  // 新增数据的时候清空数据
      this.$router.push({
        name: "Add",
        query: { param: this.form },
      })
    },
    handleEdit(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.$router.push({
        name: "Add",
        query: { param: this.form },
      })
    },
    // handleEdit1(row) {   // 编辑数据
    //   this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
    //   this.$router.push({
    //     name: "Click",
    //     query: { param: this.form },
    //   })
    // },
    loadNotice() {
      this.$request.get('/units').then(res => {
        if (res.code === '200') {
          this.noticeData = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    //监听pagesize改变的事件
    handleSizeChange(val) {
      this.pageSize = val
      this.loadNotice()
    },
    //监听页码值改变的事件
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.id ? '/units/update' : 'units/insert',
            method: this.form.id ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.$router.push('/notice')      //跳转回主页面
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', { type: "warning" }).then(response => {
        this.$request.delete('units/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/units', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          unitName: this.unitName,
          unitCategory: this.unitCategory,
        }
      }).then(res => {
        this.tableData = res.data?.rows
        this.total = res.data?.total
      })
    },
    reset() {
      this.unitName = null
      this.unitCategory = null
      this.load(1)
    },
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)
    },
  }
}
</script>

<style scoped></style>
